package com.zxw.java基础.线程.join;

/**
 * @projectName: util-cloud
 * @package: com.zxw.java基础.线程.join
 * @className: JoinExample
 * @author: zhangxuewei
 * @description: TODO
 * @date: 2023/10/31 13:33
 * @version: 1.0
 */
public class JoinExample {
    public static void main(String[] args) throws InterruptedException {
        Thread thread1 = new Thread(() -> {
            try {
                System.out.println("Thread 1 started");
                Thread.sleep(2000); // 模拟耗时操作
                System.out.println("Thread 1 finished");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread thread2 = new Thread(() -> {
            try {
                System.out.println("Thread 2 started");
                Thread.sleep(1000); // 模拟耗时操作
                System.out.println("Thread 2 finished");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        thread1.start(); // 启动线程1
        thread2.start(); // 启动线程2

        thread1.join(); // 等待线程1执行完毕
        thread2.join(); // 等待线程2执行完毕

        // 所有线程执行完毕后执行的操作
        System.out.println("All threads finished");
    }
}
